#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>

#include "libradarssd.h"

unsigned long elapsed(struct timeval* begin, struct timeval* end){
    long seconds = end->tv_sec - begin->tv_sec;
    long microseconds = end->tv_usec - begin->tv_usec;
    return seconds*1e6 + microseconds;
}

int main(int argc, char *argv[]){
    if(argc <= 1){
        printf("argment error, trace path not provided!\n");
        return 0;
    }

    char* path = argv[1];
    printf("Write trace:%s\n",path);

    FILE* fp;
    fp = fopen(path, "r");

    if (fp != NULL)
    {
        struct timeval begin, end;
        gettimeofday(&begin, 0);

        u64 write_signal = 0;
        u64 toa;
        u64 start_toa=0;
        while(!feof(fp)){ //读文件
            fscanf(fp,"%lu\n", &toa);
            //printf("read:%lu\n",toa);
            if(write_signal == 0)
                start_toa = toa;
            push_signal(toa,toa+1,toa+2);
            write_signal++;
        }

        gettimeofday(&end, 0);
        fclose(fp);
        printf("total elapsed %lu us\n",elapsed(&begin, &end));
        printf("TOA range:[%lu,%lu]\n",start_toa,toa);

        float MB = write_signal*FEATURE_SIZE*FEATURE_COUNT / (float)(1024*1024);
        float bandwith = MB/((double)elapsed(&begin, &end)/1000000);
        double average_latency = (double)elapsed(&begin, &end)/write_signal;
        printf("write %lu signals in %.2f MB\nBandwith:%.2f MB/s\nAverage latency:%.4lf us/signal\n",write_signal,MB,bandwith,average_latency);
    }
    else
        printf("fail to open! \n");

    return 0;
}
